home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / GroupsPaneModel.py < prev    next >
Text File  |  2009-10-19  |  7KB  |  210 lines

  1. ## Copyright (C) 2008 Rui Matos <tiagomatos@gmail.com>
  2.  
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2 of the License, or
  6. ## (at your option) any later version.
  7.  
  8. ## This program is distributed in the hope that it will be useful,
  9. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. ## GNU General Public License for more details.
  12.  
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this program; if not, write to the Free Software
  15. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  
  17. import gobject
  18. import gtk
  19. import libxml2
  20. from XmlHelper import xml_helper
  21. from SearchCriterion import *
  22. from gettext import gettext as _
  23.  
  24. from debug import *
  25.  
  26. class GroupsPaneItem (gobject.GObject):
  27.     def __init__ (self):
  28.         super (GroupsPaneItem, self).__init__ ()
  29.  
  30.         self.icon = None
  31.         self.name = None
  32.         self.separator = False
  33.  
  34.     def load_icon (self, icon_name):
  35.         theme = gtk.icon_theme_get_default ()
  36.         try:
  37.             return theme.load_icon (icon_name,
  38.                                     gtk.ICON_SIZE_MENU, 0)
  39.         except gobject.GError:
  40.             return None
  41.  
  42. class AllPrintersItem (GroupsPaneItem):
  43.     def __init__ (self):
  44.         super (AllPrintersItem, self).__init__ ()
  45.  
  46.         self.icon = self.load_icon ('printer')
  47.         self.name = _("All Printers")
  48.  
  49. class SeparatorItem (GroupsPaneItem):
  50.     def __init__ (self):
  51.         super (SeparatorItem, self).__init__ ()
  52.  
  53.         self.separator = True
  54.  
  55. class FavouritesItem (GroupsPaneItem):
  56.     def __init__ (self):
  57.         super (FavouritesItem, self).__init__ ()
  58.  
  59.         self.icon = self.load_icon ('emblem-favorite')
  60.         self.name = _("Favorites")
  61.  
  62. # Helper common base class, do not instantiate
  63. class MutableItem (GroupsPaneItem):
  64.     def __init__ (self, name, xml_node = None):
  65.         super (MutableItem, self).__init__ ()
  66.  
  67.         self.name = name
  68.         self.xml_node = xml_node
  69.  
  70.     def rename (self, new_name):
  71.         self.xml_node.setProp ("name", new_name)
  72.         xml_helper.write ()
  73.  
  74.         self.name = new_name
  75.  
  76.     def delete (self):
  77.         self.xml_node.unlinkNode ()
  78.         self.xml_node.freeNode ()
  79.         xml_helper.write ()
  80.  
  81. class StaticGroupItem (MutableItem):
  82.     def __init__ (self, name, xml_node = None):
  83.         super (StaticGroupItem, self).__init__ (name, xml_node)
  84.  
  85.         self.icon = self.load_icon ('folder')
  86.         self.printer_queues = []
  87.  
  88.         if not self.xml_node:
  89.             self.xml_node = libxml2.newNode ("static-group")
  90.             self.xml_node.newProp ("name", self.name)
  91.             self.xml_node.newChild (None, "queues", None)
  92.             xml_helper.add_group (self.xml_node)
  93.         else:
  94.             if not self.xml_node.children.children:
  95.                 # no queues
  96.                 return
  97.             else:
  98.                 queue_node = self.xml_node.children.children
  99.                 while queue_node:
  100.                     self.printer_queues.append (queue_node.prop ("name"))
  101.                     queue_node = queue_node.next
  102.  
  103.     def add_queues (self, queue_list):
  104.         queues_node = self.xml_node.children
  105.  
  106.         for queue_name in queue_list:
  107.             if queue_name not in self.printer_queues:
  108.                 queue_node = libxml2.newNode ("queue")
  109.                 queue_node.newProp ("name", queue_name)
  110.                 queues_node.addChild (queue_node)
  111.                 self.printer_queues.append (queue_name)
  112.  
  113.         xml_helper.write ()
  114.  
  115.     def remove_queues (self, queue_list):
  116.         queues_node = self.xml_node.children
  117.  
  118.         for queue_name in queue_list:
  119.             if queue_name in self.printer_queues:
  120.                 queue_node = self.xml_node.children.children
  121.                 while queue_node:
  122.                     if queue_node.prop ("name") == queue_name:
  123.                         break
  124.                     queue_node = queue_node.next
  125.                 queue_node.unlinkNode ()
  126.                 queue_node.freeNode ()
  127.                 self.printer_queues.remove (queue_name)
  128.  
  129.         xml_helper.write ()
  130.  
  131. class SavedSearchGroupItem (MutableItem):
  132.     def __init__ (self, name, criteria = [],
  133.                   match_all = False, xml_node = None):
  134.         super (SavedSearchGroupItem, self).__init__ (name, xml_node)
  135.  
  136.         self.icon = self.load_icon ('folder-saved-search')
  137.         self.criteria = criteria
  138.         self.match_all = match_all
  139.  
  140.         if not self.xml_node:
  141.             self.xml_node = libxml2.newNode ("search-group")
  142.             self.xml_node.newProp ("name", self.name)
  143.             criteria_node = self.xml_node.newChild (None, "criterias", None)
  144.             criteria_node.newProp ("match", self.match_all and "all" or "any")
  145.             for criterion in self.criteria:
  146.                 criterion_node = criteria_node.newChild (None, "criteria", None)
  147.                 criterion_node.newChild (None, "subject",
  148.                                          str (criterion.subject))
  149.                 criterion_node.newChild (None, "rule",
  150.                                          str (criterion.rule))
  151.                 criterion_node.newChild (None, "value",
  152.                                          str (criterion.value))
  153.             xml_helper.add_group (self.xml_node)
  154.         else:
  155.             criteria_node = self.xml_node.children
  156.             self.match_all = criteria_node.prop ("match") == "all"
  157.             criterion_node = criteria_node.children
  158.             while criterion_node:
  159.                 criterion = SearchCriterion ()
  160.  
  161.                 crit_child = criterion_node.children
  162.                 while crit_child:
  163.                     if crit_child.name == "subject":
  164.                         criterion.subject = int (crit_child.content)
  165.                     elif crit_child.name == "rule":
  166.                         criterion.rule = int (crit_child.content)
  167.                     elif crit_child.name == "value":
  168.                         criterion.value = crit_child.content
  169.                     else:
  170.                         pass
  171.                     crit_child = crit_child.next
  172.  
  173.                 self.criteria.append (criterion)
  174.                 criterion_node = criterion_node.next
  175.  
  176. class GroupsPaneModel (gtk.ListStore):
  177.     def __init__ (self):
  178.         super (GroupsPaneModel, self).__init__ (GroupsPaneItem)
  179.  
  180.     def append (self, item):
  181.         return super (GroupsPaneModel, self).append ([item])
  182.  
  183.     def get (self, iter_or_path):
  184.         return self[iter_or_path][0]
  185.  
  186.     def lookup_by_name (self, name):
  187.         for item in self:
  188.             if name == item[0].name:
  189.                 return item[0]
  190.  
  191.         return None
  192.  
  193.     def append_by_type (self, new_item):
  194.         new_item_type = type (new_item)
  195.  
  196.         titer = self.get_iter_first ()
  197.         while titer:
  198.             if type (self.get_value (titer, 0)) == new_item_type:
  199.                 break
  200.  
  201.             titer = self.iter_next (titer)
  202.  
  203.         while titer:
  204.             if type (self.get_value (titer, 0)) != new_item_type:
  205.                 break
  206.  
  207.             titer = self.iter_next (titer)
  208.  
  209.         return self.insert_before (titer, [new_item])
  210.